package com.example.ycdyus.dailyfruit.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.ycdyus.dailyfruit.BaseApp;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.bean.Items;
import com.example.ycdyus.dailyfruit.db.Good;
import com.example.ycdyus.dailyfruit.utils.ImageLoaderHelper;
import com.lidroid.xutils.exception.DbException;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by ycdyus on 2015/10/21.
 */
public class ShopListViewAdater extends CommonAdapter<Good> {
    private DisplayImageOptions options;
    private GoodsChangeCallBack callBack;
    private Context context;

    public ShopListViewAdater(Context context, List<Good> mDatas, int itemLayoutId) {
        super(context, mDatas, itemLayoutId);
        options = ImageLoaderHelper.getDisplayImageLoader();
        this.context = context;
    }

    @Override
    public void convert(ViewHolders holder, final Good item) {
        ImageLoader.getInstance().displayImage(item.getPhotos(), (ImageView) holder.getView(R.id.iv_item_shop_img), options);
        ((TextView) holder.getView(R.id.tv_item_shop_name)).setText(item.getName());
        ((TextView) holder.getView(R.id.tv_item_shop_price)).setText(item.getSale_price());
        ((TextView) holder.getView(R.id.tv_item_shop_spec)).setText(item.getSpec());
        final Button btn = (Button) holder.getView(R.id.btn_item_shop_num);
        btn.setText(item.getNum() + "");
        ((Button) holder.getView(R.id.btn_item_shop_jia)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateData(item, item.getNum(), 0, btn);
            }
        });
        ((Button) holder.getView(R.id.btn_item_shop_jian)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateData(item, item.getNum(), 1, btn);
            }
        });
    }

    private void updateData(Good item, int num, int type, Button btn) {
        if (type == 0) {
            num++;
        } else {
            if (num != 0) {
                num--;
            } else {
                showDialog(item);
            }
        }
        btn.setText(num + "");
        item.setNum(num);
        try {
            BaseApp.getInstance().getDbUtils().update(item, "num");
        } catch (DbException e) {
            e.printStackTrace();
        }
        callBack.goodsChangeCallBack(1);
    }

    private void showDialog(final Good item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("删除？");
        builder.setMessage("删除该商品？");
        builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    BaseApp.getInstance().getDbUtils().delete(item);
                    callBack.goodsChangeCallBack(1);callBack.goodsChangeCallBack(1);
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
        builder.setNegativeButton("不了", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

    public GoodsChangeCallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(GoodsChangeCallBack callBack) {
        this.callBack = callBack;
    }

    public interface GoodsChangeCallBack {
        void goodsChangeCallBack(int state);
    }
}

